package com.cbtx.module.common.vm

import android.app.Application
import android.content.Intent
import android.text.TextUtils
import androidx.lifecycle.MutableLiveData
import com.cbtx.module.base.http.HttpManager
import com.cbtx.module.base.http.MyRequestCallBack
import com.cbtx.module.base.vm.BaseVM
import com.cbtx.module.common.bean.LoginRsBean
import com.cbtx.module.common.bean.request.LoginRqBean
import com.cbtx.module.common.http.HttpUrlManager
import com.google.gson.Gson
import com.txcb.lib.base.utils.DeviceUtil
import com.txcb.lib.base.utils.MacUtil
import com.txcb.lib.base.utils.SharedPreferencesUtils
import com.txcb.lib.base.utils.ToastUtil

class LoginVM(application: Application) : BaseVM(application) {

    var loginType = ""

    var mLoginRqBean: LoginRqBean? = null
    val loginResult: MutableLiveData<Boolean> = MutableLiveData()
    var isNeedInputMarker = false

    companion object {
        const val LOGIN_PERSONAL = "login_personal"
        const val LOGIN_COMPANY = "login_company"
    }


    fun getIntent(intent: Intent) {
        val mAndroidId = DeviceUtil.getSerialNumber(mApplication)
        loginType = intent.getStringExtra("loginType")!!
        val launchAreaId = intent.getIntExtra("launchAreaId", 0)

        mLoginRqBean = LoginRqBean()
        mLoginRqBean?.deviceNo = mAndroidId

        if (loginType == LOGIN_PERSONAL) {
            mLoginRqBean?.launchAreaId = 0
        } else {
            isNeedInputMarker = true
            mLoginRqBean?.launchAreaId = launchAreaId
        }
    }


    /**
     * @param loginMemberNo 伯乐号
     * @param loginPhone    绑定的手机
     */
    fun loginRq(loginMemberNo: String, loginPhone: String, marker: String) {

        var url = ""
        if (isNeedInputMarker) {
            //公司投放
            url = HttpUrlManager.LOGIN_DEVICE_COMPANY
            mLoginRqBean?.roomCode = marker
            mLoginRqBean?.remarks = marker
            mLoginRqBean?.macAddress = MacUtil.getMac()
        } else {
            //个人投放
            url = HttpUrlManager.LOGIN_DEVICE
            mLoginRqBean?.loginMemberNo = loginMemberNo
            mLoginRqBean?.loginPhone = loginPhone
            mLoginRqBean?.deviceUid = MacUtil.getMac()
        }

        val rqJson = Gson().toJson(mLoginRqBean)
        HttpManager.getInstance("",null)
            .sendPostJson(url, rqJson, object :
                MyRequestCallBack<LoginRsBean>() {
                override fun onSuccess(loginRsBean: LoginRsBean?) {
                    if (loginRsBean != null) {
                        if (!TextUtils.isEmpty(loginRsBean.data)) {
                            SharedPreferencesUtils.saveString("authorization", loginRsBean.data)
                            SharedPreferencesUtils.saveint("areaId", mLoginRqBean?.launchAreaId!!)
                            loginResult.value = true
                        } else {
                            ToastUtil.showToast(loginRsBean.msg)
                            loginResult.value = false
                        }
                    } else {
                        ToastUtil.showToast("登录失败")
                        loginResult.value = false
                    }
                }

                override fun onFailure(error: Int, msg: String?) {
                    ToastUtil.showToast(msg)
                    loginResult.value = false
                }
            })
    }
}